import 'dart:async';
import 'package:flutter/material.dart';

class TimeTest extends StatefulWidget {
  TimeTest({Key key}) : super(key: key);
  @override
  _TimeTestState createState() => _TimeTestState();
}

class _TimeTestState extends State<TimeTest> {
  Timer _timer;
  Key _txt;

  @override
  void dispose() {
    super.dispose();
    // _timer?.cancel();
    print('timeTest dispose');
  }

  @override
  Widget build(BuildContext context) {
    return _buildContainer();
  //  return Container();
  }
  _buildContainer() {
    _setDelay(5, () {
      _timer.cancel();
      _onEnd();
    });
    return Container(
      child: Text('${widget.key}..$_txt...$_timer'),
    );
  }
  _setDelay(int time, Function cb) {
    _timer = Timer(Duration(seconds: time), () {
      _timer.cancel();
      cb();
    });
  }
  _onEnd() {
    setState(() {
      _txt = UniqueKey();
    });
    print('time out');
  }
}
